// SlideNavigator.java
package exercise.jslide.jslide.view.components;

import exercise.jslide.jslide.view.EditorView;
import exercise.jslide.jslide.view.MainView;
import javafx.scene.control.ListView;
import javafx.scene.control.ListCell;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import exercise.jslide.jslide.model.Slide;
import exercise.jslide.jslide.app.AppContext;

import java.util.List;

public class SlideNavigator extends VBox {
    private ListView<Slide> slideListView;
    private AppContext appContext;
    private MainView view;
    private static final double THUMBNAIL_WIDTH = 180;
    private static final double THUMBNAIL_HEIGHT = 120;

    public SlideNavigator(MainView view) {
        this.view = view;
        appContext = AppContext.getInstance();
        initializeComponents();
        setupEventHandlers();
    }

    private void initializeComponents() {
        slideListView = new ListView<>();
//        slideListView.setPrefWidth(THUMBNAIL_WIDTH + 20);
        VBox.setVgrow(slideListView, Priority.ALWAYS);


        // 设置自定义单元格工厂
        slideListView.setCellFactory(param -> new SlideListCell());

        // 将ListView添加到VBox
        getChildren().add(slideListView);

        // 绑定数据
        updateSlideList();
    }

    private void setupEventHandlers() {
        // 选择幻灯片时的处理
        slideListView.getSelectionModel().selectedIndexProperty().addListener((obs, oldVal, newVal) -> {
            if (newVal != null && newVal.intValue() >= 0) {
                appContext.getCurrentPresentation().setCurrentSlideIndex(newVal.intValue());
                // 触发幻灯片切换事件
                this.fireSlideChangedEvent();
            }
        });
    }

    private void fireSlideChangedEvent() {
        int currentIndex = appContext.getCurrentPresentation().getCurrentSlideIndex();
        view.getEditorView().redraw();
        view.getStatusBar().updateSlideNumber(currentIndex + 1, appContext.getCurrentPresentation().getSlides().size());
    }

    public void updateSlideList() {
        slideListView.getItems().setAll(appContext.getCurrentPresentation().getSlides());
        int currentIndex = appContext.getCurrentPresentation().getCurrentSlideIndex();
        slideListView.getSelectionModel().select(currentIndex);
        this.fireSlideChangedEvent();
    }


    public void updateSlides(List<Slide> slides) {
        slideListView.getItems().setAll(slides);
    }

    public void addSlide(Slide slide) {
        appContext.getCurrentPresentation().addSlide(slide);
        updateSlideList();
    }

    public void removeSlide(Slide slide) {
        appContext.getCurrentPresentation().removeSlide(slide);
        updateSlideList();
    }

    // 自定义ListCell用于显示幻灯片缩略图
    private class SlideListCell extends ListCell<Slide> {
        private Canvas thumbnail;
        private VBox container;
        private Label pageNumber;

        public SlideListCell() {
            thumbnail = new Canvas(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
            pageNumber = new Label();
            container = new VBox(5); // 5px spacing
            container.getChildren().addAll(thumbnail, pageNumber);
        }

        @Override
        protected void updateItem(Slide slide, boolean empty) {
            super.updateItem(slide, empty);

            if (empty || slide == null) {
                setGraphic(null);
            } else {
                // 更新页码
                int index = getIndex() + 1;
                pageNumber.setText("第 " + index + " 页");

                // 绘制缩略图
                GraphicsContext gc = thumbnail.getGraphicsContext2D();
                gc.clearRect(0, 0, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);

                // 绘制背景
                gc.setFill(javafx.scene.paint.Color.WHITE);
                gc.fillRect(0, 0, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);

                // 绘制边框
                gc.setStroke(javafx.scene.paint.Color.GRAY);
                gc.strokeRect(0, 0, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);

                // 获取幻灯片内容的宽高
                double slideWidth = view.getEditorView().getWidth();
                double slideHeight = view.getEditorView().getHeight();

                // 计算缩放比例
                double scaleX = THUMBNAIL_WIDTH / slideWidth;
                double scaleY = THUMBNAIL_HEIGHT / slideHeight;
                double scale = Math.min(scaleX, scaleY); // 使用最小的缩放比例

                // 计算居中位置
                double offsetX = (THUMBNAIL_WIDTH - slideWidth * scale) / 2;
                double offsetY = (THUMBNAIL_HEIGHT - slideHeight * scale) / 2;

                // 应用缩放和位移
                gc.save();
                gc.translate(offsetX, offsetY);
                gc.scale(scale, scale);

                // 绘制幻灯片内容的缩略图
                slide.drawThumbnail(gc);

                gc.restore();

                setGraphic(container);
            }
        }
    }
}